package edu.uic.kott.activities;

import java.util.List;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Compute;
import edu.uic.kott.model.Course;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CalendarContract.Attendees;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class CourseDetails extends Activity {

	private static final String LOGTAG = "KOTT";

	KottDataSource datasource;

	private String id;
	private String className;
	private String startDate;
	private String endDate;
	private String days;
	private String startTime;
	private String endTime;
	private String tWSS;
	private String instructor;
	private String ta;
	private String classLocation;
	
	private int attCount;
	private double attPercentage;

	private TextView courseNameTextView;
	private TextView instructorTextView;
	private TextView taTextView;
	private TextView daysTextView;
	private TextView timeTextView;

	private EditText weeklyAttEditText;
	private EditText termAttEditText;
	private EditText targetedWssEditText;
	private EditText weeklySsEditText;
	private EditText termSSEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_course_details);

		courseNameTextView = (TextView) findViewById(R.id.rCourseNameTextView);
		instructorTextView = (TextView) findViewById(R.id.rInstructorNameTextView);
		taTextView = (TextView) findViewById(R.id.rTaNameTextView);
		daysTextView = (TextView) findViewById(R.id.rDaysTextView);
		timeTextView = (TextView) findViewById(R.id.rTimeTextView);

		weeklyAttEditText = (EditText) findViewById(R.id.weeklyAttEditText);
		termAttEditText = (EditText) findViewById(R.id.termAttEditText);
		targetedWssEditText = (EditText) findViewById(R.id.targetedWssEditText);
		weeklySsEditText = (EditText) findViewById(R.id.weeklySsEditText);
		termSSEditText = (EditText) findViewById(R.id.termSSEditText);		

		datasource = new KottDataSource(this);
		Bundle extras = getIntent().getExtras();
		Course course = new Course();

		if (extras != null){
			id = extras.getString("courseId");
			Log.i(LOGTAG,"Course id is:" + id);

			//insert code here
			course = datasource.getCourseDetails(id);

			className = course.getCourseName();
			startDate = course.getStartDate();
			endDate = course.getEndDate();
			days = course.getClassDays();
			startTime = course.getStartTime();
			endTime = course.getEndTime();
			tWSS = course.getTargetedWSS();
			instructor = course.getInstructor();
			ta = course.getTa();
			classLocation = course.getClassLocation();
			Log.i(LOGTAG, "class location is " + classLocation);

			courseNameTextView.setText(className);
			instructorTextView.setText(instructor);
			taTextView.setText(ta);
			daysTextView.setText(days);
			timeTextView.setText(startTime + "  to  "  + endTime);
			targetedWssEditText.setText(tWSS);			
		}
		
		
		Compute c = new Compute();
		List<String> termToDateList = c.termToDateList(startDate, endDate, days);
		
		Log.i(LOGTAG, "term to date count " + termToDateList.size());
		
		List<String> totaltermAtendedList = datasource.datesAttended(id);
		Log.i(LOGTAG, "term to date attended count " + totaltermAtendedList.size());
		attCount = 0;
		//attPercentage = 0;
		
		for (String attendedDate : totaltermAtendedList) {
			if(termToDateList.contains(attendedDate))
				attCount++;
		}
		
		attPercentage = (attCount/termToDateList.size())*100;
		termAttEditText.setText(String.valueOf(attCount));		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_edit_delete, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if(item.getOrder() == 100){			
			Log.i(LOGTAG,"EDIT menu item clicked");
			Intent intent = new Intent(CourseDetails.this, AddEditCourse.class);
			intent.putExtra("option", "update");
			intent.putExtra("id", id);
			intent.putExtra("className",className);
			intent.putExtra("startDate", startDate);
			intent.putExtra("endDate", endDate);
			intent.putExtra("days", days);
			intent.putExtra("startTime", startTime);
			intent.putExtra("endTime", endTime);
			intent.putExtra("tWSS", tWSS);
			intent.putExtra("classLocation", classLocation);
			intent.putExtra("instructor", instructor);
			intent.putExtra("ta", ta);
			startActivity(intent);
			finish();
		}

		else if(item.getOrder() == 200)
		{
			Log.i(LOGTAG,"Calling DELETE course from Course Details class");
			AlertDialog.Builder builder = new AlertDialog.Builder(CourseDetails.this);
			builder.setTitle(R.string.dialogTitle_delete);		//set message title
			builder.setMessage(R.string.dialogMsg_deleteCourse); //set message content

			builder.setCancelable(true);
			builder.setNegativeButton(R.string.cancel, null);

			builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(datasource.deleteCourse(id)){
						
						//int count = datasource.removeDates(id);
						//Log.i(LOGTAG, "Count of dates deleted" + String.valueOf(count));
						finish();
					}					
				}
			});

			AlertDialog confirmDialog = builder.create();
			confirmDialog.show();
		}
		return super.onOptionsItemSelected(item);
	}

}
